<?php


/*
*   project snock
*  xml  opreration
*   author king-of-war  date  2009-05-10
*   using class simpleXML
*/
class xml_op {
	private $url;
	function __construct($url) {
		$this->set_url($url);

	}
	/*
	*  open xml file with $filename
	*/
	function xml_vote($filename, $title, $items) {

		$url = $this->get_url();
		if (file_exists($url . $filename)) {

			$xml = simplexml_load_file($url . $filename);

			/*
			* vote  set
			*
			*/
			//name
			$xml->pie[0]->pie_mcname = 'liujie';
			//title
			$xml->pie[0]->pie_title[0] = $title . ',200,40,16,true,0x333333';
			//pie_percentshow
			$xml->pie[0]->pie_percentshow = 'all';

			/*
			*  items set
			*
			*/
			$colors = array (
				'0xFF0F00',
				'0xFF6600',
				'0xFF9E00',
				'0xFCD202',
				'0xF8FF01',
				'0xB0DE09',
				'0x04D215'
			);

			$max = count($items['name']);
			echo $max;
			for ($i = 0; $i < $max; $i++) {
				if ($i != $max -1) {
					$qu = ',';

					$bool = 'true';
				} else {
					$qu = '';
					$bool = 'false';
				}
				$names .= $items['name'][$i] . $qu;
				$date .= $items['date'][$i] . $qu;
				$color .= $colors[$i] . $qu;
				$showtitle .= $bool . $qu;
				$defaultsate .= $bool . $qu;
			}

			$xml->pie[0]->pie_num = $date;
			$xml->pie[0]->pie_name = $names;
			$xml->pie[0]->pie_color = $color;
			$xml->pie[0]->pie_showtitle = $showtitle;
			$xml->pie[0]->pie_defaultsate = $defaultsate;

			/*
			* write xml
			*/

			$contant = $xml->asXML();

			$this->xml_creat('includes/xml/piedata.xml', $contant);

		} else {
			exit ('Failed to open ' . $url . $filename);
		}

	}

	/*
	 * set admins infomation
	 *
	 */
	function xml_admin_set($id, $name,$right, $role, $mail) {
		$url = $this->get_url();
		$file = 'includes/xml/users.xml';
		if (file_exists($url . $file)) {

			$xml = simplexml_load_file($url . $file);

			//name
			$xml->user[0]->id = $id;
			$xml->user[0]->name = $name;
			$xml->user[0]->right = $right;
			$xml->user[0]->role = $role;
			$xml->user[0]->E_mail = $mail;

			$contant = $xml->asXML();

			$this->xml_creat($file, $contant);

			return true;
		} else {
			echo 'File open error <br/>Check the file please';
			return false;
			exit;
		}

	}

	/*
		 * get admins infomation
		 *
		 */
	function xml_admin_get() {
		$url = $this->get_url();
		$file = 'includes/xml/users.xml';
		if (file_exists($url . $file)) {

			$xml = simplexml_load_file($url . $file);

			//name
			$users['id'] = $xml->user[0]->id;
			$users['name'] = $xml->user[0]->name;
			$users['right'] = $xml->user[0]->right;
			$users['role'] = $xml->user[0]->role;
			$users['mail'] = $xml->user[0]->E_mail;

			$contant = $xml->asXML();

			$this->xml_creat($file, $contant);

			return $users;
		} else {
			echo 'File open error <br/>Check the file please';
			return false;
			exit;
		}

	}

	/*
	*  xml write
	*
	*/
	function xml_creat($filename, $contant) {
		$url = $this->get_url();
		$fp = fopen($url . $filename, w);
		if (!fp) {
			echo 'file create or open fail , check the right please';
		}
		if (!empty ($contant)) {
			fwrite($fp, $contant);
		} else {
			echo 'null contant';
		}
		if (!fclose($fp))
			echo 'close fail';
	}

	function set_url($url) {
		$this->url = $url;
	}
	function get_url() {
		return $this->url;
	}

}
?>